package com.tb;

import java.util.regex.Pattern;

/**
 * comment
 *
 * @author zhao yuan
 * @since 2018/3/27
 */
public class IpWildcardMatcher {

    public static  boolean match(String pattern, String ip){
        if(isWildcard(pattern)){
            String s = convertPattern(pattern);
            return ip.matches(s);
        }

        return pattern.equals(ip);
    }

    public static  boolean isWildcard(String pattern){
        return (pattern.contains("?") || pattern.contains("*"));
    }

    public static String convertPattern(String pattern){
        return pattern.replaceAll("\\.", "\\.").replaceAll("\\?",".").replaceAll("\\*", "[^.]+");
    }
}
